<?php

    // TITLE: Main Index Controller
    // FILE: index.php
    // AUTHOR: des301

    date_default_timezone_set('America/New_York'); // move to config file
    
    // REQUIRES //////////
    require ('model/dbio.php');
    //require ('model/gateway.php');
    require ('class/link.php');

    // CONSTANTS //////////
    DEFINE ('TAB', "\t");
    DEFINE ('RETURN', "\n");
    DEFINE ('DS', 'DIRECTORY_SEPERATOR');
    DEFINE ('APPDIR', 'application/');
    
    // DEFINITIONS //////////
    $tab = "\t";
    $return = '<br>'; //"\n";

    // GLOBALS //////////
    global $application;
    global $styles;
    global $scripts;
    global $dir;
    global $sub;
    global $act;
    global $msg;
    
    // INSTANTIATE IOs //////////
    $dbio = new DBIO();
    // $ffio = new FFIO();
    // $ssio = new SSIO();

    // APPLICATION ROUTING //////////
    $dir = (isset($_GET['dir'])) ? $_GET['dir'] : 'apps';
    $sub = (isset($_GET['sub'])) ? $_GET['sub'] : false;
    $act = (isset($_GET['act'])) ? $_GET['act'] : false;
    $msg = (isset($_GET['msg'])) ? $_GET['msg'] : false;
    
    $application = ($appId) ? $dbio->readApp($appId) : false;
    
    // STYLE AND SCRIPT FILES //////////
    $styles = array();
    $scripts = array();
    $styleFile = APPDIR . $dir . '/style.php';
    $scriptFile = APPDIR . $dir . '/script.php';
    $styles = array('css/layout.css', 'css/style.css', 'css/button.css');
    if (file_exists($styleFile)) {include $styleFile;}
    if (file_exists($scriptFile)) {include $scriptFile;}
    
    // CONTROLLER //////////
    include (APPDIR . $dir . '/controller/' . (($sub) ? $sub : $dir) . '.php');

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

        <!-- STYLE SHEETS -->

        <link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,400,700' rel='stylesheet' type='text/css'>
        <?php foreach ($styles as $style) {echo '<link rel="stylesheet" href="' . APPDIR . $style . '">' . "\n";} ?>

        <!-- SCRIPTS -->

        <?php foreach ($scripts as $script) {echo '<script src="' . APPDIR . $script . '"></script>' . "\n";} ?>

    </head>
    <body>
        <?php include ('root/head.php'); ?>
        <div id="page">

            <div id="side">
                <?php include ('root/nav.php'); ?>

                <dl id="info">
                    <dt>EST</dt>
                    <dd><?php echo date('h:i:s'); ?></dd>
                    <dt>Client IP</dt>
                    <dd><?php echo $_SERVER["REMOTE_ADDR"]; ?></dd>
                    <dt>Server IP</dt>
                    <dd><?php echo $_SERVER["SERVER_ADDR"]; ?></dd>
                </dl>

            </div>

            <div id="content">
                <?php
                    //if ($msg) echo '<span class="alert">' . $msg . '</span>';
                    //include ($dir . '/controller/' . $goto . '.php');
                    include APPDIR . $page;
                ?>
            </div>
            <?php include ('root/foot.php'); ?>
        </div>
    </body>
</html>
